package com.umeox.ota.engine;

import com.umeox.ota.service.OtaService;
import com.umeox.ota.utils.ActivityCollector;
import com.umeox.ota.data.constant.TRIGGER;
import com.umeox.ota.gloable.App;
import com.umeox.ota.utils.NotifyUtils;
import com.umeox.ota.utils.Utils;
import com.abupdate.iot_libs.data.local.OtaEntity;
import com.abupdate.iot_libs.data.remote.VersionInfo;
import com.abupdate.iot_libs.engine.DataManager;
import com.abupdate.iot_libs.engine.otaStatus.OtaStatusMgr;
import com.abupdate.iot_libs.engine.policy.PolicyManager;
import com.abupdate.iot_libs.engine.security.FotaException;
import com.abupdate.iot_libs.utils.NetUtils;
import com.abupdate.iot_libs.utils.SPFTool;
import com.abupdate.trace.Trace;

import java.util.List;

import static com.umeox.ota.mvvm.viewModel.MainViewModel.SP_IS_LOCAL_UPDATE;

/**
 * @author fighter_lee
 * @date 2018/8/30
 */
public class NewVersionDispatcher {

    private static final String TAG = "NewVersionDispatcher";

    public static void onReceiverNewVersion(List<VersionInfo> list) {
        Trace.i(TAG, "onReceiverNewVersion() new version:" + list.get(0).versionAlias);

        if (ActivityCollector.isShowing()) {
            Trace.d(TAG, "FOTA App is displayed in the foreground, will ignore this notify");
            return;
        }
        SPFTool.putBoolean(SP_IS_LOCAL_UPDATE, false);
        DataManager dataManager = ((App) App.sCx).getRepositoryComponent().repositoryManager().obtainDataManager(DataManager.class);
        OtaEntity mainEntity;
        try {
            mainEntity = dataManager.getMainEntity();
        } catch (FotaException e) {
            e.printStackTrace();
            return;
        }

        OtaStatusMgr otaStatusMgr = OtaStatusMgr.getInstance();
        PolicyManager policyManager = mainEntity.getPolicyManager();
        if (otaStatusMgr.isUpgradeFail()) {
            Trace.d(TAG, "upgrade failed will check version");
            OtaService.startService(OtaService.ACTION_CHECK_VERSION_TASK, TRIGGER.CHECK.CHECK_CLIENT.name());
        } else if (otaStatusMgr.isCheckNewVersion() || otaStatusMgr.isDownloadFailed() || otaStatusMgr.isDownloadCancelled()) {
            //根据策略判断是否强制下载
            if (policyManager.isDownloadForce()) {
                boolean canDownload = !policyManager.isRequestWifi() || NetUtils.NETWORK_WIFI.equals(NetUtils.getNetworkState(App.sCx));
                //如果当前设备的网络为wifi，或者策略为任意网络都能下载
                if (canDownload) {
                    Trace.d(TAG, "onReceiverNewVersion() is Download Force and not request wifi");
                    OtaService.startService(OtaService.ACTION_DOWNLOAD_TASK, TRIGGER.DOWNLOAD.DOWNLOAD_AUTO.name());
                    return;
                }
                //强制下载但是 WiFi 策略不满足时也不提示
                Trace.d(TAG, "Download force condition not satisfied");
                return;
            }
            Trace.d(TAG, "onReceiverNewVersion() will notice new version");
            NotifyUtils.notifyNewVersion();
        } else if (otaStatusMgr.isDownloadFinished()) {
            //根据策略判断是否强制升级
            if (policyManager.isForceInstall()) {
                Trace.d(TAG, "onReceiverNewVersion() is Install Force");
                OtaService.startService(OtaService.ACTION_INSTALL_TASK, TRIGGER.UPGRADE.UPGRADE_FORCE.name());
                return;
            }
            Trace.d(TAG, "onReceiverNewVersion() will notice install");
            NotifyUtils.notifyDownloadFinish(false);
        } else if (otaStatusMgr.isNeedReboot()) {
            //根据策略判断是否强制升级
            if (policyManager.isForceInstall()) {
                Trace.d(TAG, "Install force, will reboot");
                Utils.reboot();
                return;
            }
            NotifyUtils.notifyAbupdateSuccess();
            Trace.d(TAG, "Reboot condition not satisfied");
        }
    }
}
